import time
import re
from common.loger_handler import mylog
from page_element.key import PageKey
from common.ftp_handler import total_p_v


class page_flight(PageKey):
    """任务飞行测试"""


    def flight_001(self,name):
        """
        航线飞行:
        我手动绘制了航点任务和动作
        确认无人机是否会完成这些动作
        通过获取高度，速度，俯仰角度，进度百分比，FTP照片张数进行判断
        """
        time.sleep(2)
        self.driver(resourceId="com.gdu.pro2:id/ivTaskFlyBtn").click()
        time.sleep(1)
        self.driver.xpath("//*[contains(@text,'收藏航线')]").click()
        time.sleep(1)
        if self.driver.xpath(F"//*[contains(@text,'{name}')]").exists:
            self.driver.xpath(F"//*[contains(@text,'{name}')]").click()
        else:
            self.driver.swipe(956, 1088, 960, 588)
            time.sleep(2)
            self.driver.xpath(F"//*[contains(@text,'{name}')]").click()
        time.sleep(2)
        self.driver(resourceId="com.gdu.pro2:id/img_begin_fly").click()
        time.sleep(1)
        if self.driver(resourceId="com.gdu.pro2:id/tv_photo_count").exists():
            self.driver.click(1236, 691)
        time.sleep(2)
        self.driver.swipe(1213, 999, 1274, 226)
        time.sleep(2)
        self.driver(resourceId="com.gdu.pro2:id/btnNextStep").click()
        time.sleep(1)
        self.driver.xpath("//*[contains(@text,'上传航迹')]").click()
        time.sleep(5)
        self.driver.xpath("//*[contains(@text,'开始飞行')]").click()
        time.sleep(20)
        self.driver(resourceId="com.gdu.pro2:id/left_control_layout").click()
        tv_height_list = []
        x_speed_list = []
        tv_angle_list = []
        tv_progress_list = []
        while True:
            try:
                tv_height = self.driver(resourceId="com.gdu.pro2:id/tv_height").get_text()[:-1]   # 高度
                redt = self.driver(resourceId="com.gdu.pro2:id/tv_arlink").get_text()
                x_speed = redt.split('；')[2][6:]   # 水平速度
                tv_angle = redt.split('；')[3][8:]  # 云台角度
                tv_progress = self.driver(resourceId="com.gdu.pro2:id/tv_progress").get_text()   # 任务进度百分比
                mylog.info(F"飞行高度：{tv_height}------水平飞行速度：{x_speed}------云台角度：{tv_angle}-----任务进度：{tv_progress}")
                tv_height_list.append(tv_height)
                x_speed_list.append(x_speed)
                tv_angle_list.append(tv_angle)
                tv_progress_list.append(tv_progress)
                time.sleep(1)
            except BaseException:
                break
        mylog.info(tv_height_list)  # 用于判断高度，是否相符
        mylog.info(x_speed_list)    # 用于判断悬停 连续20条，判断速度'0.0m/s
        mylog.info(tv_angle_list)   # 用于判断云台角度，俯仰设置
        mylog.info(tv_progress_list)   # 用于判断是否执行完成
        mylog.info(int(total_p_v()['.jpg']))     # 判断照片张数
        mylog.info(int(total_p_v()['.mp4']))     # 判断视频条数
        time.sleep(10)
        if self.driver(resourceId="com.gdu.pro2:id/break_layout").exists():
            mylog.info("任务完成，飞机开始返航")
        local = time.time()
        while True:
            if self.driver(resourceId="com.gdu.pro2:id/tv_height").exists():
                height = self.driver(resourceId="com.gdu.pro2:id/tv_height").get_text()
                if height == "0.00m":
                    mylog.info("任务完成，降落成功")
                    break
                else:
                    mylog.info(F"还在降落中，当前高度：{height}")
                time.sleep(5)
            if time.time() - local > 300:
                break
        self.driver.press("back")
        time.sleep(1)
        self.driver.press("back")

        return [int(total_p_v()['.jpg']),int(total_p_v()['.mp4']), tv_progress_list, x_speed_list, tv_height_list, tv_angle_list]   # 实际拍照数， 实际视频数，进度列表，悬停期间速度

    def flight_002(self, name):
        """
        二维，三维，航带，倾斜摄影飞行:
        我手动绘制了航线任务
        通过获取高度，速度，俯仰角度，进度百分比，FTP照片张数进行判断
        """
        # 开启任务
        time.sleep(2)
        self.driver(resourceId="com.gdu.pro2:id/ivTaskFlyBtn").click()
        time.sleep(1)
        self.driver.xpath("//*[contains(@text,'收藏航线')]").click()
        time.sleep(1)
        if self.driver.xpath(F"//*[contains(@text,'{name}')]").exists:
            self.driver.xpath(F"//*[contains(@text,'{name}')]").click()
        else:
            self.driver.swipe(956, 1088, 960, 588)
            time.sleep(2)
            self.driver.xpath(F"//*[contains(@text,'{name}')]").click()
        time.sleep(2)
        if name == "自动化倾斜摄影":
            numbre1 = int(self.driver(resourceId="com.gdu.pro2:id/tv_photo_count").get_text())
            self.driver(resourceId="com.gdu.pro2:id/fiveWayTwo").click()
            time.sleep(1)
            numbre2 = int(self.driver(resourceId="com.gdu.pro2:id/tv_photo_count").get_text())
            self.driver(resourceId="com.gdu.pro2:id/fiveWayThree").click()
            time.sleep(1)
            numbre3 = int(self.driver(resourceId="com.gdu.pro2:id/tv_photo_count").get_text())
            self.driver(resourceId="com.gdu.pro2:id/fiveWayFour").click()
            time.sleep(1)
            numbre4 = int(self.driver(resourceId="com.gdu.pro2:id/tv_photo_count").get_text())
            self.driver(resourceId="com.gdu.pro2:id/fiveWayFive").click()
            time.sleep(1)
            numbre5 = int(self.driver(resourceId="com.gdu.pro2:id/tv_photo_count").get_text())
            numbre = numbre1 + numbre2 + numbre3 + numbre4 + numbre5
        else:
            numbre = int(self.driver(resourceId="com.gdu.pro2:id/tv_photo_count").get_text())
        time.sleep(1)
        self.driver(resourceId="com.gdu.pro2:id/img_begin_fly").click()
        time.sleep(2)
        if name == "自动化航带飞行":
            if self.driver(resourceId="com.gdu.pro2:id/scrollView").exists():
                self.driver.click(1236, 691)
            time.sleep(2)
            self.driver.click(1027, 745)
            time.sleep(1)
            self.driver(resourceId="com.gdu.pro2:id/tv_strip_selected_count").click()
            time.sleep(2)
        elif name == "自动化倾斜摄影":
            if self.driver(resourceId="com.gdu.pro2:id/scrollView").exists():
                self.driver.click(1236, 691)
            time.sleep(2)
            self.driver(resourceId="com.gdu.pro2:id/tvConfirm").click()
            time.sleep(2)
        else:
            if self.driver(resourceId="com.gdu.pro2:id/scrollView").exists():
                self.driver.click(1236, 691)
            time.sleep(2)

        self.driver.swipe(1213, 999, 1274, 226)
        time.sleep(2)
        self.driver(resourceId="com.gdu.pro2:id/btnNextStep").click()
        time.sleep(1)
        self.driver.xpath("//*[contains(@text,'上传航迹')]").click()
        time.sleep(5)
        self.driver.xpath("//*[contains(@text,'开始飞行')]").click()
        mylog.info("----------无人机开始飞行----------")
        time.sleep(30)

        # 循环判断进度
        tv_progress_list = []
        x_speed_list = []
        x = 0
        while True:
            try:
                tv_progress = self.driver(resourceId="com.gdu.pro2:id/tv_progress").get_text()  # 任务进度百分比
                tv_progress_list.append(tv_progress)
                mylog.info(F"----------飞行进度：{tv_progress}----------")
                time.sleep(3)
                if int(tv_progress[:-1]) >= 50 and x == 0:
                    x = 1  # 一次性判断，不再进入此判断
                    time.sleep(1)
                    self.driver(resourceId="com.gdu.pro2:id/img_pause_or_resume_route").click()
                    mylog.info(F"----------暂停任务----------")
                    time.sleep(6)
                    while True:
                        redt = self.driver(resourceId="com.gdu.pro2:id/tv_arlink").get_text()
                        x_speed = redt.split('；')[2][6:]  # 水平速度
                        x_speed_list.append(x_speed)
                        time.sleep(1)
                        if len(x_speed_list) >= 10:
                            break
                    mylog.info(F"----------暂停任务期间：水平速度：{x_speed_list}")
                    self.driver(resourceId="com.gdu.pro2:id/img_pause_or_resume_route").click()
                    mylog.info(F"----------重启任务----------")
                    time.sleep(1)
                    self.driver(resourceId="com.gdu.pro2:id/btnConfirm").click()
            except BaseException:
                break

        # 结束判断
        mylog.info(tv_progress_list)  # 用于判断是否执行完成
        mylog.info(total_p_v()['.jpg'])     # 判断照片张数
        time.sleep(10)
        self.driver(resourceId="com.gdu.pro2:id/left_control_layout").click()
        time.sleep(2)
        local = time.time()
        while True:
            if self.driver(resourceId="com.gdu.pro2:id/tv_height").exists():
                height = self.driver(resourceId="com.gdu.pro2:id/tv_height").get_text()
                if height == "0.00m":
                    mylog.info("任务完成，降落成功")
                    break
                else:
                    mylog.info(F"还在降落中，当前高度：{height}")
                time.sleep(5)
            if time.time() - local > 300:
                break
        self.driver.press("back")
        time.sleep(1)
        self.driver.press("back")

        return [numbre, int(total_p_v()['.jpg']),tv_progress_list, x_speed_list]  # 预期拍照数， 实际拍照数，进度列表，悬停期间速度








